package net.kuratkoo.jidelnicekvut;

import android.content.Context;
import android.database.Cursor;
import java.io.IOException;
import java.sql.Time;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Locale;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class CanteenSet  extends ArrayList<Canteen> {

    private static final String TAG = "JidelnicekVUT|CanteenSet";
    private Context context;

    public CanteenSet(Context context) {
        this.context = context;
     }

    public CanteenSet getAll() {
        this.clear();
        DatabaseAdapter db = new DatabaseAdapter(this.context);
        Cursor c = db.getDb().rawQuery("SELECT _id FROM canteen c LEFT JOIN canteen_star cs ON cs.canteen_id = c._id WHERE c.valid = 1", null);
        if (c.getCount() > 0) {
            while (c.moveToNext()) {
                Canteen canteen = new Canteen(this.context);
                canteen.getByCanteenId(c.getInt(c.getColumnIndex("_id")));
                if (canteen.isShowable()) {
                    this.add(canteen);
                }
            }
        }
        c.close();
        db.close();
        this.sort();
        return this;
    }

    public Canteen getByCanteenId(Integer canteenId) {
        Canteen canteen = null;
        for (Canteen c : this) {
            if (canteenId == c.canteenId) {
                canteen = c;
                break;
            }
        }
        return canteen;
    }

    private CanteenSet sort() {
        if (Preferences.getString(context, "order", "star").equals("name")) {
            Collections.sort(this, new Comparator<Canteen>() {

                public int compare(Canteen m1, Canteen m2) {
                    Collator c = Collator.getInstance(new Locale("cs", "CZ"));
                    return c.compare(m1.name, m2.name);
                }
            });
        } else if (Preferences.getString(context, "order", "star").equals("star")) {
            Collections.sort(this, new Comparator<Canteen>() {

                public int compare(Canteen m1, Canteen m2) {
                    return m2.star.compareTo(m1.star);
                }
            });
        } else if (Preferences.getString(context, "order", "star").equals("open")) {
            Collections.sort(this, new Comparator<Canteen>() {

                public int compare(Canteen m1, Canteen m2) {
                    return m2.isOpen().compareTo(m1.isOpen());
                }
            });
        }
        return this;
    }

    public static void sync(Context c) throws MyHttpClientException, ParserConfigurationException, SAXException, IOException {
        DatabaseAdapter db = new DatabaseAdapter(c);

        MyHttpClient mhc = new MyHttpClient(Constants.CANTEEN_URL, "windows-1250");
        mhc.execute();
        Document doc = XML.stringToXml(mhc.getResponse());
        NodeList rows = doc.getElementsByTagName("row");

        db.getDb().delete("canteen", null, null);

        for (int i = 0; i < rows.getLength(); i++) {
            Element row = (Element) rows.item(i);
            Canteen canteen = new Canteen(c);
            canteen.canteenId = Integer.parseInt(XML.getValue(row, "id"));

            // Insert or update canteen
            canteen.name = XML.getValue(row, "nazev");
            canteen.guid = XML.getValue(row, "GUID");
            canteen.address = XML.getValue(row, "adresa");
            if (Integer.parseInt(XML.getValue(row, "poradi")) != 0) {
                canteen.valid = true;
            } else {
                canteen.valid = false;
            }
            canteen.save();
        }

        // Load canteen opening hours
        MyHttpClient mhco = new MyHttpClient(Constants.CANTEEN_OPENING_HOUR_URL, "windows-1250");
        mhco.execute();
        doc = XML.stringToXml(mhco.getResponse());
        rows = doc.getElementsByTagName("row");

        db.getDb().delete("canteen_hours", null, null);

        for (int i = 0; i < rows.getLength(); i++) {
            Element row = (Element) rows.item(i);
            CanteenHour ch = new CanteenHour(c);
            ch.canteenId = Integer.parseInt(XML.getValue(row, "id"));
            ch.day = Integer.parseInt(XML.getValue(row, "den"));
            ch.from = new Time(Integer.parseInt(XML.getValue(row, "odH")), Integer.parseInt(XML.getValue(row, "odM")), 0);
            ch.to = new Time(Integer.parseInt(XML.getValue(row, "doH")), Integer.parseInt(XML.getValue(row, "doM")), 0);
            ch.save();
        }
        db.close();
    }
}
